package com.wj.service.impl;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSON;
import com.wj.domain.dto.BookDTO;
import com.wj.domain.entity.BookEntity;
import com.wj.domain.entity.ChapterContentEntity;
import com.wj.domain.entity.ChapterEntity;
import com.wj.domain.vo.BookVO;
import com.wj.domain.vo.NetworkConfigVO;
import com.wj.infrastructure.mapper.BookMapper;
import com.wj.infrastructure.mapper.ChapterMapper;
import com.wj.infrastructure.mapper.NetworkConfigMapper;
import com.wj.infrastructure.repository.BookRepository;
import com.wj.infrastructure.repository.ChapterContentRepository;
import com.wj.infrastructure.repository.ChapterRepository;
import com.wj.service.WriteBookService;
import com.wj.tool.common.exception.SelfException;
import com.wj.tool.common.utils.CollectionUtils;
import com.wj.tool.common.utils.StringUtils;
import com.wj.tool.common.utils.UuidUtils;
import com.wj.tool.utils.JsoupUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.http.client.utils.DateUtils;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Optional;


@Service
@Slf4j
public class WriteBookServiceImpl implements WriteBookService {

    @Resource
    private BookMapper bookMapper;

    @Resource
    private BookRepository bookRepository;

    @Resource
    private ChapterContentRepository chapterContentRepository;

    @Resource
    private ChapterRepository chapterRepository;

    @Resource
    private ChapterMapper chapterMapper;

    @Resource
    private NetworkConfigMapper networkConfigMapper;

    @Value("${file.absopath_pre}")
    private String file_absopath_pre;

    @Value("${file.realpath_pre}")
    private String file_realpath_pre;

    @PersistenceContext
    private EntityManager entityManager;

    @Resource
    private JdbcTemplate jdbcTemplate;

    /**
     * 抓取小说持久化到mysql数据库
     * @param bookUrl
     */
    @Async
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void insert(String bookUrl) {
        //爬取小说基本信息
        BookEntity bookEntity = new BookEntity();
        Document doc = JsoupUtil.getDoc(bookUrl);
        BookDTO bookDTO = null;
        String urlKeyWord = "";
        if (bookUrl.contains("xbiquge")){
            bookDTO = getBiqugeFic(doc);
            urlKeyWord = "xbiquge";
        } else if (bookUrl.contains("shuquge")) {
            bookDTO = getShuqugeFic(doc);
            urlKeyWord = "shuquge";
        } else if (bookUrl.contains("xbiquwx")){
            bookDTO = getXbiquwx(doc);
            urlKeyWord = "xbiquwx";
        } else if (bookUrl.contains("biqugeg.cc")){
            bookDTO = getBiqugeFic(doc);
            urlKeyWord = "biqugeg";
        } else if (bookUrl.contains("xibiquge.com")){
            bookDTO = getXibiquge(doc);
            urlKeyWord = "xibiquge";
        } else {
            bookDTO = getOther(doc);
        }
        if (bookDTO != null) {
            //查询数据库信息
            List<BookVO> list = bookMapper.queryByUrl(bookUrl);
            if (CollectionUtils.isEmpty(list)){
                BeanUtils.copyProperties(bookDTO, bookEntity);
                String localImgUrl = getOnlineUrlToLocal(bookEntity.getBookName(), bookDTO.getImgUrl());
                bookEntity.setImgUrl(localImgUrl);
                Long bookId = UuidUtils.createId();
                bookEntity.setId(bookId);
                //设置url
                bookEntity.setBookUrl(bookUrl);
                Date now = new Date();
                bookEntity.setCreateUser(-1l);
                bookEntity.setCreateTime(now);
                bookEntity.setUpdateUser(-1l);
                bookEntity.setUpdateTime(now);
                bookEntity.setDeleted(false);
                bookRepository.save(bookEntity);

                Elements elements = doc.select("dd>a");
                int real_num = elements.size();
                //前面12章节重复
                int k = 12;
                //数据库排序
                int sort = 1;
                //前面12章节重复
                int j = elements.size();
                int real_i = 0;
                if (bookUrl.contains("shuquge") || bookUrl.contains("biqugeg.cc")){
                    real_i += k;
                    real_num -= k ;
                }
                if (bookUrl.contains("xibiquge.com")){
                    // 前面9章重复
                    k = 9;
                    real_i += k;
                    real_num -= k ;
                }

                log.info("小说章数：" + real_num);

                List<ChapterContentEntity> chapterContentEntityList = new ArrayList<>();
                List<ChapterEntity> chapterEntityList = new ArrayList<>();
                for (int i = real_i; i < j; i++) {
                    ChapterContentEntity chapterContentEntity=new ChapterContentEntity();
                    try {
                        String url = elements.get(i).attr("abs:href");
                        Document document = JsoupUtil.getDoc(url);
                        //章节标题
                        String title = document.select("h1").text();
                        //章节内容
                        String text = JsoupUtil.subContent(document.getElementById("content").html());
                        //保存小说内容
                        Long chapterContentId = UuidUtils.createId();
                        chapterContentEntity.setId(chapterContentId);
                        chapterContentEntity.setBookId(bookId);
                        chapterContentEntity.setContent(text);
                        chapterContentEntityList.add(chapterContentEntity);
                        //chapterContentRepository.save(chapterContent);
                        ChapterEntity chapterEntity = new ChapterEntity();
                        chapterEntity.setId(UuidUtils.createId());
                        chapterEntity.setChapterUrl(url);
                        chapterEntity.setBookId(bookId);
                        chapterEntity.setChapterTitle(title);
                        chapterEntity.setContentId(chapterContentId);
                        chapterEntity.setSort(sort);
                        chapterEntity.setCreateTime(new Date());
                        chapterEntityList.add(chapterEntity);
                        //chapterRepository.save(chapterEntity);
                        log.info("抓取小说：" + bookDTO.getBookName() + "" + title);
                        sort++;
                        k++;
                    }catch (Exception e){
                        log.error("抓取小说内容异常"+e);
                    }
                    if (CollectionUtils.isNotEmpty(chapterContentEntityList)){
                        chapterContentRepository.saveAll(chapterContentEntityList);
                    }
                    if (CollectionUtils.isNotEmpty(chapterEntityList)){
                        chapterRepository.saveAll(chapterEntityList);
                    }
                }

                log.info("抓取小说完成");
            } else {
                BookVO ft = list.get(0);

                int countChapter = chapterMapper.countChapter(ft.getId());
                if (countChapter >= 0){
                    Long bookId = ft.getId();
                    //设置url
                    //设置url
                    Optional<BookEntity> optional = bookRepository.findById(ft.getId());
                    if (optional.isPresent()){
                        bookEntity = optional.get();
                        bookEntity.setBookUrl(bookUrl);
                        Date now = new Date();
                        bookEntity.setUpdateUser(-1l);
                        bookEntity.setUpdateTime(now);
                        bookRepository.save(bookEntity);
                    }
                    Elements elements = doc.select("dd>a");
                    log.info("线上小说总章数：" + elements.size()+"，当前数据库已存在"+countChapter+"章");
                    //前面12章节重复
                    int k = 12;
                    //数据库排序
                    int sort = countChapter + 1;
                    //前面12章节重复
                    int j = elements.size();
                    int beginChap = countChapter;
                    if (bookUrl.contains("shuquge") || bookUrl.contains("biqugeg.cc")) {
                        j = j - k;
                        beginChap = countChapter + k;
                    }
                    if (bookUrl.contains("xibiquge.com")){
                        // 前面9章重复
                        k = 9;
                        j = j - k;
                        beginChap = countChapter + k;
                    }

                    if (j > countChapter){

                        List<ChapterContentEntity> chapterContentEntityList = new ArrayList<>();
                        List<ChapterEntity> chapterEntityList = new ArrayList<>();
                        for (int i = beginChap; i < elements.size(); i++) {
                            ChapterContentEntity chapterContentEntity=new ChapterContentEntity();
                            try {
                                String url = elements.get(i).attr("abs:href");
                                Document document = JsoupUtil.getDoc(url);
                                //章节标题
                                String title = document.select("h1").text();
                                //章节内容
                                String text = JsoupUtil.subContent(document.getElementById("content").html());
                                //保存小说内容
                                Long chapterContentId = UuidUtils.createId();
                                chapterContentEntity.setId(chapterContentId);
                                chapterContentEntity.setBookId(bookId);
                                chapterContentEntity.setContent(text);
                                chapterContentEntityList.add(chapterContentEntity);
                                //chapterContentRepository.save(chapterContent);
                                ChapterEntity chapterEntity = new ChapterEntity();
                                chapterEntity.setId(UuidUtils.createId());
                                chapterEntity.setChapterUrl(url);
                                chapterEntity.setBookId(bookId);
                                chapterEntity.setChapterTitle(title);
                                chapterEntity.setContentId(chapterContentId);
                                chapterEntity.setSort(sort);
                                chapterEntity.setCreateTime(new Date());
                                chapterEntityList.add(chapterEntity);
                                //chapterRepository.save(chapterEntity);
                                log.info("抓取小说：" + bookDTO.getBookName() + "" + title);
                                sort++;
                                k++;
                            }catch (Exception e){
                                log.error("抓取小说内容异常"+e);
                            }
                        }
                        if (CollectionUtils.isNotEmpty(chapterContentEntityList)){
                            chapterContentRepository.saveAll(chapterContentEntityList);
                        }
                        if (CollectionUtils.isNotEmpty(chapterEntityList)){
                            chapterRepository.saveAll(chapterEntityList);
                        }
                    }

                    log.info("抓取小说完成");
                }
            }

        } else {
            log.info("小说连接地址错误");
        }
    }

    public String getOnlineUrlToLocal(String bookName, String onlineUrl){
        String localUlr = "";
        URL url = null;
        FileOutputStream fileOutputStream =null;
        InputStream inputStream =null;
        try {
            url = new URL(onlineUrl);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.addRequestProperty("User-Agent","Mozilla/5.0 (Windows NT 6.1; WOW64; rv:55.0) Gecko/20100101 Firefox/55.0");
            connection.setConnectTimeout(10 * 1000);
            connection.setReadTimeout(15 * 1000);
            inputStream = connection.getInputStream();
            byte[] buffer = new byte[1024];
            int length;
            SimpleDateFormat sdf = new SimpleDateFormat("YYYYMMddHHmmss");
            String newFileName =  sdf.format(new Date()) + ".jpg";
            //存储路径
            String fileAbsoPath = file_absopath_pre + bookName + "/" + bookName + newFileName;

            String fileRealpath = file_realpath_pre + bookName + "/" + bookName + newFileName;
            localUlr = fileRealpath;
            fileOutputStream = new FileOutputStream(fileAbsoPath);
            while ((length = inputStream.read(buffer)) != -1) {
                fileOutputStream.write(buffer, 0, length);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (inputStream != null){
                    inputStream.close();
                }
                if (fileOutputStream != null){
                    fileOutputStream.flush();
                    fileOutputStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return localUlr;
    }

    /**
     * 抓取小说持久化到mysql数据库
     * @param bookUrl
     */
    @Async
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void insert2(String bookUrl) {
        //爬取小说基本信息
        BookEntity bookEntity = new BookEntity();
        Document doc = JsoupUtil.getDoc(bookUrl);
        BookDTO bookDTO = null;

        NetworkConfigVO config = networkConfigMapper.getConfig(bookUrl);
        int k = 0;
        String selectClass = "dd>a";
        if (config != null){
            k = config.getNewNum();
            selectClass = config.getSelectClass();
        } else {
            throw new SelfException("");
        }
        bookDTO = getOther(doc);
        if (bookDTO != null) {
            //查询数据库信息
            List<BookVO> list = bookMapper.queryByUrl(bookUrl);
            if (CollectionUtils.isEmpty(list)){
                BeanUtils.copyProperties(bookDTO, bookEntity);
                Long bookId = UuidUtils.createId();
                bookEntity.setId(bookId);
                //设置url
                bookUrl = getOnlineUrlToLocal(bookEntity.getBookName(), bookEntity.getImgUrl());
                bookEntity.setBookUrl(bookUrl);
                Date now = new Date();
                bookEntity.setCreateUser(-1l);
                bookEntity.setCreateTime(now);
                bookEntity.setUpdateUser(-1l);
                bookEntity.setUpdateTime(now);
                bookEntity.setDeleted(false);
                bookRepository.save(bookEntity);

                Elements elements = doc.select(selectClass);
                int real_num = elements.size();
                //数据库排序
                int sort = 1;
                //前面12章节重复
                int j = elements.size();
                int real_i = 0;
                real_i += k;
                real_num -= k ;

                log.info("小说章数：" + real_num);

                List<ChapterContentEntity> chapterContentEntityList = new ArrayList<>();
                List<ChapterEntity> chapterEntityList = new ArrayList<>();
                for (int i = real_i; i < j; i++) {
                    ChapterContentEntity chapterContentEntity=new ChapterContentEntity();
                    try {
                        String url = elements.get(i).attr("abs:href");
                        Document document = JsoupUtil.getDoc(url);
                        //章节标题
                        String title = document.select("h1").text();
                        //章节内容
                        String text = JsoupUtil.subContent(document.getElementById("content").html());
                        //保存小说内容
                        Long chapterContentId = UuidUtils.createId();
                        chapterContentEntity.setId(chapterContentId);
                        chapterContentEntity.setBookId(bookId);
                        chapterContentEntity.setContent(text);
                        chapterContentEntityList.add(chapterContentEntity);
                        //chapterContentRepository.save(chapterContent);
                        ChapterEntity chapterEntity = new ChapterEntity();
                        chapterEntity.setId(UuidUtils.createId());
                        chapterEntity.setChapterUrl(url);
                        chapterEntity.setBookId(bookId);
                        chapterEntity.setChapterTitle(title);
                        chapterEntity.setContentId(chapterContentId);
                        chapterEntity.setSort(sort);
                        chapterEntity.setCreateTime(new Date());
                        chapterEntityList.add(chapterEntity);
                        //chapterRepository.save(chapterEntity);
                        log.info("抓取小说：" + bookDTO.getBookName() + "" + title);
                        sort++;
                        k++;
                    }catch (Exception e){
                        log.error("抓取小说内容异常"+e);
                    }
                    if (CollectionUtils.isNotEmpty(chapterContentEntityList)){
                        saveChapterContentBatch(chapterContentEntityList);
                    }
                    if (CollectionUtils.isNotEmpty(chapterEntityList)){
                        saveChapterBatch(chapterEntityList);
                    }
                }

                log.info("抓取小说完成");
            } else {
                BookVO ft = list.get(0);

                int countChapter = chapterMapper.countChapter(ft.getId());
                if (countChapter >= 0){
                    Long bookId = ft.getId();
                    //设置url
                    Optional<BookEntity> optional = bookRepository.findById(ft.getId());
                    if (optional.isPresent()){
                        bookEntity = optional.get();
                        bookEntity.setBookUrl(bookUrl);
                        Date now = new Date();
                        bookEntity.setUpdateUser(-1l);
                        bookEntity.setUpdateTime(now);
                        bookRepository.save(bookEntity);
                    }
                    Elements elements = doc.select(selectClass);
                    log.info("线上小说总章数：" + elements.size()+"，当前数据库已存在"+countChapter+"章");
                    //数据库排序
                    int sort = countChapter + 1;
                    //前面12章节重复
                    int j = elements.size();
                    int beginChap = countChapter + k;
                    j = j - k;
                    beginChap = countChapter + k;

                    if (j > countChapter){

                        List<ChapterContentEntity> chapterContentEntityList = new ArrayList<>();
                        List<ChapterEntity> chapterEntityList = new ArrayList<>();
                        for (int i = beginChap; i < elements.size(); i++) {
                            ChapterContentEntity chapterContentEntity=new ChapterContentEntity();
                            try {
                                String url = elements.get(i).attr("abs:href");
                                Document document = JsoupUtil.getDoc(url);
                                //章节标题
                                String title = document.select("h1").text();
                                //章节内容
                                String text = JsoupUtil.subContent(document.getElementById("content").html());
                                //保存小说内容
                                Long chapterContentId = UuidUtils.createId();
                                chapterContentEntity.setId(chapterContentId);
                                chapterContentEntity.setBookId(bookId);
                                chapterContentEntity.setContent(text);
                                chapterContentEntityList.add(chapterContentEntity);
                                //chapterContentRepository.save(chapterContent);
                                ChapterEntity chapterEntity = new ChapterEntity();
                                chapterEntity.setId(UuidUtils.createId());
                                chapterEntity.setChapterUrl(url);
                                chapterEntity.setBookId(bookId);
                                chapterEntity.setChapterTitle(title);
                                chapterEntity.setContentId(chapterContentId);
                                chapterEntity.setSort(sort);
                                chapterEntity.setCreateTime(new Date());
                                chapterEntityList.add(chapterEntity);
                                //chapterRepository.save(chapterEntity);
                                log.info("抓取小说：" + bookDTO.getBookName() + "" + title);
                                sort++;
                                k++;
                            }catch (Exception e){
                                log.error("抓取小说内容异常"+e);
                            }
                        }
                        if (CollectionUtils.isNotEmpty(chapterContentEntityList)){
                            saveChapterContentBatch(chapterContentEntityList);
                        }
                        if (CollectionUtils.isNotEmpty(chapterEntityList)){
                            saveChapterBatch(chapterEntityList);
                        }
                    }

                    log.info("抓取小说完成");
                }
            }

        } else {
            log.info("小说连接地址错误");
        }
    }

    /**
     * 批量插入
     *
     * @param list 实体类集合
     * @param <T>  表对应的实体类
     */
    public <T> void batchInsert(List<T> list) {
        if (!ObjectUtils.isEmpty(list)) {
            for (int i = 0; i < list.size(); i++) {
                entityManager.persist(list.get(i));
                if (i % 50 == 0) {
                    entityManager.flush();
                    entityManager.clear();
                }
            }
            entityManager.flush();
            entityManager.clear();
        }
    }

    /**
     * 批量更新
     *
     * @param list 实体类集合
     * @param <T>  表对应的实体类
     */
    public <T> void batchUpdate(List<T> list) {
        if (!ObjectUtils.isEmpty(list)) {
            for (int i = 0; i < list.size(); i++) {
                entityManager.merge(list.get(i));
                if (i % 50 == 0) {
                    entityManager.flush();
                    entityManager.clear();
                }
            }
            entityManager.flush();
            entityManager.clear();
        }
    }

    public void saveChapterBatch(List<ChapterEntity> list) {
        List<ChapterEntity> saveList = new ArrayList<>();
        Date now = new Date();
        int i=0;
        for (ChapterEntity entity : list) {
            i++;
            entity.setCreateTime(now);
            saveList.add(entity);
            //一千条执行一次
            if(i >= 1000){
                chapterRepository.saveAll(saveList);
                i = 0;
                saveList = new ArrayList<>();
            }
        }
        if(i > 0 && CollectionUtils.isNotEmpty(saveList)){
            chapterRepository.saveAll(saveList);
        }

    }

    public StringBuilder intoChapter(){
        StringBuilder insert = new StringBuilder("INSERT INTO `chapter` (`id`, `book_id`, " +
                " `chapter_title`, `content_id`,`sort`,`chapter_url`,`create_time`)" +
                " VALUES ");
        return insert;
    }

    public void saveChapterContentBatch(List<ChapterContentEntity> list) {
        StringBuilder sb = intoChapterContent();
        int i=0;
        List<ChapterContentEntity> saveList = new ArrayList<>();
        for (ChapterContentEntity entity : list) {
            i++;
            saveList.add(entity);
            //一千条执行一次
            if(i >= 1000){
                chapterContentRepository.saveAll(saveList);
                i = 0;
                saveList = new ArrayList<>();
            }
        }
        if(i > 0 && CollectionUtils.isNotEmpty(saveList)){
            chapterContentRepository.saveAll(saveList);
        }

    }

    public StringBuilder intoChapterContent(){
        StringBuilder insert = new StringBuilder("INSERT INTO `chapter_content` (`id`, `book_id`, `content`)" +
                " VALUES ");
        return insert;
    }

    private BookDTO getXibiquge(Document document) {
        boolean flag = false;
        try {
            String bookName = document.select("meta[property=\"og:title\"]").attr("content");
            String bookDesc = document.select("meta[property=og:description]").attr("content");
            String author = document.select("meta[property=og:novel:author]").attr("content");
            String typeName = document.select("meta[property=og:novel:category]").attr("content");
            Integer type = getTypeByName(typeName);
            String newest = document.select("meta[property=og:novel:latest_chapter_name]").attr("content");
            String state = document.select("meta[property=og:novel:status]").attr("content");
            String img = document.select("div>img").attr("abs:src");
            if (StringUtils.isBlank(state)){
                state = "连载中";
            }
            //小说基本信息
            Elements small = document.select("div#list");
            //字数
            Integer number = 0;//JsoupUtil.sub(small.get(0).child(3).text());
            BookDTO bookDTO = new BookDTO("", img, bookDesc, bookName, author, type, newest, state, number);
            return bookDTO;
        } catch (Exception e) {
            log.error("获取小说基本信息失败"+e);
        }
        return null;
    }

    private BookDTO getOther(Document document) {
        boolean flag = false;
        try {
            String bookName = document.select("meta[property=\"og:title\"]").attr("content");
            String bookDesc = document.select("meta[property=og:description]").attr("content");
            String author = document.select("meta[property=og:novel:author]").attr("content");
            String typeName = document.select("meta[property=og:novel:category]").attr("content");
            Integer type = getTypeByName(typeName);
            String newest = document.select("meta[property=og:novel:latest_chapter_name]").attr("content");
            String state = document.select("meta[property=og:novel:status]").attr("content");
            String img = document.select("div>img").attr("abs:src");
            if (StringUtils.isBlank(state)){
                state = "连载中";
            }
            //小说基本信息
            Elements small = document.select("div#list");
            //字数
            Integer number = 0;//JsoupUtil.sub(small.get(0).child(3).text());
            BookDTO bookDTO = new BookDTO("", img, bookDesc, bookName, author, type, newest, state, number);
            return bookDTO;
        } catch (Exception e) {
            log.error("获取小说基本信息失败"+e);
        }
        return null;
    }

    private BookDTO getXbiquwx(Document document) {
        boolean flag = false;
        try {
            String bookName = document.select("meta[property=\"og:title\"]").attr("content");
            String bookDesc = document.select("meta[property=og:description]").attr("content");
            String author = document.select("meta[property=og:novel:author]").attr("content");
            String typeName = document.select("meta[property=og:novel:category]").attr("content");
            Integer type = getTypeByName(typeName);
            String newest = document.select("meta[property=og:novel:latest_chapter_name]").attr("content");
            String state = document.select("meta[property=og:novel:status]").attr("content");
            String img = document.select("div>img").attr("abs:src");
            if (StringUtils.isBlank(state)){
                state = "连载中";
            }
            //小说基本信息
            Elements small = document.select("div#list");
            //字数
            Integer number = 0;//JsoupUtil.sub(small.get(0).child(3).text());
            BookDTO bookDTO = new BookDTO("", img, bookDesc, bookName, author, type, newest, state, number);
            return bookDTO;
        } catch (Exception e) {
            log.error("获取小说基本信息失败"+e);
        }
        return null;
    }

    /**
     * 获取书趣阁小说基本信息
     * @param document
     * @return
     */
    public BookDTO getBooks(Document document) {
        boolean flag = false;
        try {
            String bookName = document.select("meta[property=og:novel:book_name]").attr("content");
            String bookDesc = document.select("meta[property=og:description]").attr("content");
            String author = document.select("meta[property=og:novel:author]").attr("content");
            String typeName = document.select("meta[property=og:novel:category]").attr("content");
            Integer type = getTypeByName(typeName);
            String newest = document.select("meta[property=og:novel:latest_chapter_name]").attr("content");
            String state = document.select("meta[property=og:novel:status]").attr("content");
            String img = document.select("div>img").attr("abs:src");
            //小说基本信息
            Elements small = document.select("div.small");
            //字数
            Integer number = Integer.valueOf(JsoupUtil.sub(small.get(0).child(3).text()));
            return new BookDTO("", img, bookDesc, bookName, author, type, newest, state, number);
        } catch (Exception e) {
            log.error("获取小说基本信息失败"+e);
        }
        return null;
    }

    public BookDTO getShuqugeFic(Document document){
        boolean flag = false;
        try {
            String bookName = document.select("meta[property=og:novel:book_name]").attr("content");
            String bookDesc = document.select("meta[property=og:description]").attr("content");
            String author = document.select("meta[property=og:novel:author]").attr("content");
            String typeName = document.select("meta[property=og:novel:category]").attr("content");
            Integer type = getTypeByName(typeName);
            String newest = document.select("meta[property=og:novel:latest_chapter_name]").attr("content");
            String state = document.select("meta[property=og:novel:status]").attr("content");
            String img = document.select("div>img").attr("abs:src");
            if (StringUtils.isBlank(state)){
                state = "连载中";
            }
            //小说基本信息
            Elements small = document.select("div.small");
            //字数
            Integer number = Integer.valueOf(JsoupUtil.sub(small.get(0).child(3).text()));
            BookDTO bookDTO = new BookDTO("", img, bookDesc, bookName, author, type, newest, state, number);
            return bookDTO;
        } catch (Exception e) {
            log.error("获取小说基本信息失败"+e);
        }
        return null;
    }

    public BookDTO getBiqugeFic(Document document){
        boolean flag = false;
        try {
            String bookName = document.select("meta[property=og:novel:book_name]").attr("content");
            String bookDesc = document.select("meta[property=og:description]").attr("content");
            String author = document.select("meta[property=og:novel:author]").attr("content");
            String typeName = document.select("meta[property=og:novel:category]").attr("content");
            Integer type = getTypeByName(typeName);
            String newest = document.select("div#info").get(0).child(4).text();
            String state = document.select("meta[property=og:novel:status]").attr("content");
            String img = document.select("meta[property=og:image]").attr("content");
            //小说基本信息
            Elements small = document.select("div#list");
            //字数
            Integer number = 0;//JsoupUtil.sub(small.get(0).child(3).text());
            BookDTO bookDTO = new BookDTO("", img, bookDesc, bookName, author, type, newest, state, number);
            return bookDTO;
        } catch (Exception e) {
            log.error("获取小说基本信息失败"+e);
        }
        return null;
    }

    public int getTypeByName(String typeName){
        if (StringUtils.isBlank(typeName)){
            return 0;
        }
        if (typeName.contains("都市")){
            return 1;
        } else if (typeName.contains("玄幻")){
            return 2;
        } else if (typeName.contains("修真")){
            return 3;
        } else if (typeName.contains("科幻")){
            return 4;
        } else if (typeName.contains("网游")){
            return 5;
        } else if (typeName.contains("历史")){
            return 6;
        } else {
            return 7;
        }
    }

    public static void main(String[] args) {
        WriteBookServiceImpl w = new WriteBookServiceImpl();
        String hanXB = "https://www.xbiquge.la/24/24780/";
//        Document doc = JsoupUtil.getDoc("https://www.xbiquge.la/24/24780/");
//        w.getBiqugeFic(doc);

        String hanSQ = "https://www.shuquge.com/txt/9439/index.html";

        String fictionURL = hanXB;
        String bookUrl = "https://www.ibiquges.com/64/64016/";
        Document doc = JsoupUtil.getDocument(bookUrl);

        Elements elements = doc.select("dd>a");

        log.info("小说章数：" + elements.size());
        //前面12章节重复
        int k = 12;
        //数据库排序
        int sort = 1;
        //前面12章节重复
        int j = 1;
        if (fictionURL.contains("shuquge")) {
            j = elements.size() - 12;
        } else {
            j = elements.size();
        }
        for (int i = 0; i < j; i++) {

            String url = elements.get(i).attr("abs:href");
            Document document = JsoupUtil.getDoc(url);
            //章节标题
            String title = document.select("h1").text();
            //章节内容
            String text = JsoupUtil.subContent(document.getElementById("content").html());
            System.out.println("" + doc);
        }
        //Document doc1 = JsoupUtil.getDoc("https://www.shuquge.com/txt/9439/index.html");
        //w.insert("https://www.shuquge.com/txt/9439/index.html");
    }
}